package com.hq.modules.operate.controller;

import com.hq.common.annotation.SysLog;
import com.hq.common.utils.R;
import com.hq.modules.sys.controller.AbstractController;
import com.hq.modules.sys.dao.RegionsMapper;
import com.hq.modules.sys.entity.Regions;
import com.hq.modules.sys.entity.RegionsInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 系统用户
 */
@RestController
@RequestMapping("/mark")
public class MarkController extends AbstractController {

    @Resource
    private RegionsMapper regionsMapper;


    /**
     * 标记区域地图
     */
    @SysLog("标记区域地图")
    @PostMapping("/markRegion")
    public RegionsInfo markRegion(@RequestBody RegionsInfo regionInfo) {
        // 去除字符串中的括号和空格
        String cleanedString = regionInfo.getCenterTude().replaceAll("[\\[\\] ]", "");
        // 使用逗号分割字符串
        String[] parts = cleanedString.split(",");
        // 将分割后的字符串转换为双精度浮点数
		Regions regions = new Regions();
		BeanUtils.copyProperties(regionInfo,regions);
		regions.setLatitude(parts[0]);
		regions.setLongitude(parts[1]);
		regions.setIsDelete(0);
        regionsMapper.insertSelective(regions);
		regionInfo.setId(regions.getId());
        return regionInfo;
    }

    /**
     * 取消标记区域地图
     */
    @SysLog("取消标记区域地图")
    @PostMapping("/removeRegion")
    public R deleteRegion(@RequestBody Regions regions) {
        regions.setIsDelete(1);
        regionsMapper.updateByPrimaryKeySelective(regions);
        return R.ok();
    }
    /**
     * 修改标记区域信息
     */
    @SysLog("修改标记区域地图")
    @PostMapping("/artelRegion")
    public R artelRegion(@RequestBody Regions regions) {
        regionsMapper.updateByPrimaryKeySelective(regions);
        return R.ok();
    }
    /**
     * 查询区域地图
     */
    @SysLog("查询区域地图")
    @GetMapping("/selectAllRegion")
    public ArrayList<RegionsInfo> selectRegion() {
        List<Regions> regions = regionsMapper.findByAll();
        ArrayList<RegionsInfo> arrayList = new ArrayList<>();
        regions.forEach(r -> {
            RegionsInfo regionsInfo = RegionsInfo.builder().id(r.getId()).radius(r.getRadius()).centerTude("[" + r.getLatitude() + "," + r.getLongitude() + "]").build();
            BeanUtils.copyProperties(r,regionsInfo);
			arrayList.add(regionsInfo);
        });
        return arrayList;

    }
}
